<?php

namespace App\Console\Commands\Wanggui;

use App\Enums\OpenShop\FeishuMini\ProgrammingEnum;
use App\Models\Common\MapNationalCommunityModel;
use App\Models\Common\MapNationalIndustrialParkModel;
use App\Models\Common\MapNationalStreetModel;
use App\Models\Common\MapNationalUniversitiesModel;
use App\Models\OpenShop\Common\SiteLocationMallModel;
use App\Models\Wanggui\WangguiSearchDotModel;
use Illuminate\Console\Command;

class SyncDotSearch extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wanggui:sync-dot-search';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步点位搜索';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        ini_set('memory_limit', '1G');

        // 截断表
        WangguiSearchDotModel::query()->truncate();

        // 商场
        SiteLocationMallModel::query()
            //->where('grade', '>', 0)
            ->select(['id', 'wgNumber', 'mallName', 'address', 'province', 'city', 'district', 'provinceCode', 'cityCode', 'districtCode'])
            ->chunkById(1000, function ($malls) {
                $insertData = [];
                foreach ($malls as $mall) {
                    $insertData[] = [
                        'wgNumber'     => $mall->wgNumber,
                        'type'         => ProgrammingEnum::PC_TYPE_LIST_HEAD_MALL,
                        'address'      => $mall->mallName . "|" . $mall->address,
                        'province'     => $mall->province,
                        'city'         => $mall->city,
                        'district'     => $mall->district,
                        'provinceCode' => $mall->provinceCode,
                        'cityCode'     => $mall->cityCode,
                        'districtCode' => $mall->districtCode,
                    ];
                }
                WangguiSearchDotModel::query()->insert($insertData);
            });

        // 街道
        MapNationalStreetModel::query()
            //->where('grade', '>', 0)
            ->select(['id', 'wgNumber', 'name', 'address', 'province', 'city', 'district', 'provinceCode', 'cityCode', 'districtCode'])
            ->chunkById(1000, function ($streets) {
                $insertData = [];
                foreach ($streets as $street) {
                    $insertData[] = [
                        'wgNumber'     => $street->wgNumber,
                        'type'         => ProgrammingEnum::PC_TYPE_LIST_HEAD_STREET,
                        'address'      => $street->name . "|" . $street->address,
                        'province'     => $street->province,
                        'city'         => $street->city,
                        'district'     => $street->district,
                        'provinceCode' => $street->provinceCode,
                        'cityCode'     => $street->cityCode,
                        'districtCode' => $street->districtCode,
                    ];
                }
                WangguiSearchDotModel::query()->insert($insertData);
            });

        // 社区
        MapNationalCommunityModel::query()
            //->where('grade', '>', 0)
            ->select(['id', 'wgNumber', 'name', 'address', 'province', 'city', 'district', 'provinceCode', 'cityCode', 'districtCode'])
            ->chunkById(1000, function ($communities) {
                $insertData = [];
                foreach ($communities as $community) {
                    $insertData[] = [
                        'wgNumber'     => $community->wgNumber,
                        'type'         => ProgrammingEnum::PC_TYPE_LIST_HEAD_STREET,
                        'address'      => $community->name . "|" . $community->address,
                        'province'     => $community->province,
                        'city'         => $community->city,
                        'district'     => $community->district,
                        'provinceCode' => $community->provinceCode,
                        'cityCode'     => $community->cityCode,
                        'districtCode' => $community->districtCode,
                    ];
                }
                WangguiSearchDotModel::query()->insert($insertData);
            });

        // 产业园
        MapNationalIndustrialParkModel::query()
            //->where('grade', '>', 0)
            ->select(['id', 'wgNumber', 'name', 'province', 'city', 'district', 'provinceCode', 'cityCode', 'districtCode'])
            ->chunkById(1000, function ($parks) {
                $insertData = [];
                foreach ($parks as $park) {
                    $insertData[] = [
                        'wgNumber'     => $park->wgNumber,
                        'type'         => ProgrammingEnum::PC_TYPE_LIST_HEAD_PARK,
                        'address'      => $park->name,
                        'province'     => $park->province,
                        'city'         => $park->city,
                        'district'     => $park->district,
                        'provinceCode' => $park->provinceCode,
                        'cityCode'     => $park->cityCode,
                        'districtCode' => $park->districtCode,
                    ];
                }
                WangguiSearchDotModel::query()->insert($insertData);
            });

        // 高校
        MapNationalUniversitiesModel::query()
            //->where('grade', '>', 0)
            ->select(['id', 'wgNumber', 'name', 'province', 'city', 'district', 'provinceCode', 'cityCode', 'districtCode'])
            ->chunkById(1000, function ($universities) {
                $insertData = [];
                foreach ($universities as $university) {
                    $insertData[] = [
                        'wgNumber'     => $university->wgNumber,
                        'type'         => ProgrammingEnum::PC_TYPE_LIST_HEAD_UNIVERSITIES,
                        'address'      => $university->name,
                        'province'     => $university->province,
                        'city'         => $university->city,
                        'district'     => $university->district,
                        'provinceCode' => $university->provinceCode,
                        'cityCode'     => $university->cityCode,
                        'districtCode' => $university->districtCode,
                    ];
                }
                WangguiSearchDotModel::query()->insert($insertData);
            });

    }
}
